package liuliu.throughwaring.ui.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.dialog.LLDialog;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseFragment;
import liuliu.throughwaring.control.settting.ISettingView;
import liuliu.throughwaring.control.settting.SettingListener;
import liuliu.custom.model.CodeModel;
import liuliu.throughwaring.model.Key;
import liuliu.throughwaring.model.TerminalModel;
import liuliu.throughwaring.service.NoticeHandlerService;
import liuliu.throughwaring.ui.activity.BluetoothListActivity;
import liuliu.throughwaring.ui.activity.MainActivity;
import liuliu.throughwaring.ui.activity.ModifyPasswordActivity;
import liuliu.throughwaring.ui.activity.SettingActivity;

/**
 * 个人中心
 * 作者：柳伟杰 on 2016/3/14 15:18
 * 邮箱：1031066280@qq.com
 */
public class WodeFragment extends BaseFragment implements ISettingView {
    @CodeNote(id = R.id.tongzhi_wode_ll, click = "onClick")
    LinearLayout tongzhi_wode_ll;
    @CodeNote(id = R.id.update_mima_wode_ll, click = "onClick")
    LinearLayout update_mima_wode_ll;
    @CodeNote(id = R.id.update_zaitao_wode_ll, click = "onClick")
    LinearLayout update_zaitao_wode_ll;
    @CodeNote(id = R.id.update_sys_wode_ll, click = "onClick")
    LinearLayout update_sys_wode_ll;
    @CodeNote(id = R.id.sys_setting_wode_ll, click = "onClick")
    LinearLayout sys_setting_wode_ll;
    @CodeNote(id = R.id.lanya_setting_wode_ll, click = "onClick")
    LinearLayout lanya_setting_wode_ll;
    @CodeNote(id = R.id.about_us_wode_ll, click = "onClick")
    LinearLayout about_us_wode_ll;
    @CodeNote(id = R.id.tongzhi_wode_switch, click = "onClick")
    Switch tongzhi_wode_switch;
    @CodeNote(id = R.id.wode_xiaqu)
    TextView xiaqu;
    @CodeNote(id = R.id.wode_name)
    TextView name;
    private LLDialog llDialog;
    SettingListener listener;
    Dialog dialog;
    List<CodeModel> codeList = null;
    @CodeNote(id = R.id.wode_blueth_name)
    TextView bname;

    @Override
    public void initViews() {
        setContentView(R.layout.frag_wode);
        llDialog = new LLDialog(MainActivity.mIntails);
        listener = new SettingListener(MainActivity.mIntails, this);

    }

    public String getCodeVlaue(List<CodeModel> list, String name) {
        for (CodeModel c : list) {
            if (c.getName().equals(name)) {
                return c.getValue();
            }
        }
        return "";
    }

    @Override
    public void initEvents() {
        if (null != Utils.ReadString("BlueToothAddress")) {
            bname.setText(Utils.ReadString("BlueToothAddress"));
        }
        name.setText(Utils.ReadString(Key.KEY_POLICE_NAME));
        xiaqu.setText(Utils.ReadString(Key.KEY_STATION_NAME));
        tongzhi_wode_switch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                String s = "";
                return false;
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tongzhi_wode_ll:
                break;
            case R.id.tongzhi_wode_switch:
                tongzhiManage();
                break;
            case R.id.update_mima_wode_ll:
                //修改密码
                MainActivity.mIntails.mUtils.IntentPost(ModifyPasswordActivity.class);
                break;
            case R.id.update_zaitao_wode_ll:
                //更新在逃库
                //下载在逃人员库
                dialog = Utils.ProgressDialog(MainActivity.mIntails, dialog, "下载中，请稍候...", false);
                dialog.show();
                listener.getCards();
                break;
            case R.id.update_sys_wode_ll:
                //检查更新
                dialog = Utils.ProgressDialog(MainActivity.mIntails, dialog, "更新中，请稍候...", false);
                dialog.show();
                MainActivity.mIntails.finalDb.deleteAll(TerminalModel.class);
                listener.downLoad();
                break;
            case R.id.sys_setting_wode_ll:
                //网络设置
                MainActivity.mIntails.mUtils.IntentPost(SettingActivity.class, new Utils.putListener() {
                    @Override
                    public void put(Intent intent) {
                        intent.putExtra("Setting", "1");
                    }
                });
                break;
            case R.id.lanya_setting_wode_ll:
                //蓝牙
                Intent intent = new Intent(MainActivity.mIntails, BluetoothListActivity.class);
                startActivityForResult(intent, 100);
                break;
            case R.id.about_us_wode_ll:
                //关于我们
                break;
        }

    }


    /**
     * 打开关闭通知的方法
     *
     * @author hh 2016年4月11日 10:03:58
     */
    private void tongzhiManage() {
        if (!tongzhi_wode_switch.isChecked()) {
            llDialog.setMiddleMessage("关闭以后无法接收企业推送信息，确定要关闭？");
            llDialog.setOnPositiveListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(MainActivity.mIntails, NoticeHandlerService.class);
                    MainActivity.mIntails.stopService(intent);
                    llDialog.cancel();
                }
            });
            llDialog.setOnNegativeListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    tongzhi_wode_switch.setChecked(true);
                    llDialog.cancel();
                }
            });
            llDialog.show();
        } else {
            tongzhi_wode_switch.setChecked(true);
            Intent intent = new Intent(MainActivity.mIntails, NoticeHandlerService.class);
            MainActivity.mIntails.startService(intent);
        }
    }

    @Override
    public void downLoad(boolean isTrue, String mes) {
        dialog.dismiss();
        if (isTrue) {
            MainActivity.mIntails.mUtils.ToastShort(MainActivity.mIntails, "更新成功！");
        } else {
            MainActivity.mIntails.mUtils.ToastShort(MainActivity.mIntails, "更新失败！");
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 100) {
            if (null != data) {
                bname.setText(data.getStringExtra("BlueToothAddress"));
            }
        }
    }

    /**
     * 下载在逃人员库的结果
     *
     * @param isTrue 是否下载成功。true为成功
     */
    @Override
    public void DownKu(boolean isTrue) {
        dialog.dismiss();
        if (isTrue) {
            MainActivity.mIntails.mUtils.ToastShort(MainActivity.mIntails, "在逃人员库下载成功！");
        } else {
            MainActivity.mIntails.mUtils.ToastShort(MainActivity.mIntails, "在逃人员库下载失败！");
        }
    }
}
